package com.yingeo.ai.sdk.demo;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class PayDialog extends Dialog implements DialogInterface.OnDismissListener {
    private TextView downTimerView;

    private final String money;


    public PayDialog(@NonNull Context context, String money) {
        super(context, R.style.dialog_style);
        this.money = "￥" + money;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.dialog_pay);
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        View rootView = findViewById(R.id.pay_root_view);
        rootView.getLayoutParams().width = (int) (dm.widthPixels * 0.28F);
        setOnDismissListener(this);
        downTimerView = findViewById(R.id.down_timer);
        initMoney(money);
        downTimer.start();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        downTimer.cancel();
    }

    private void initMoney(String money) {
        TextView tv = findViewById(R.id.money1);
        tv.setText(money);
        tv = findViewById(R.id.money2);
        tv.setText(money);
        tv = findViewById(R.id.money3);
        tv.setText(money);
    }

    private final CountDownTimer downTimer = new CountDownTimer(4000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            long second = millisUntilFinished / 1000;
            downTimerView.setText(String.format("%s秒后关闭", second));
        }

        @Override
        public void onFinish() {
            dismiss();
        }
    };
}
